﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
   public class OrdersService
    {
        //新增部分
        public static bool AddOrders(Orders or)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_UsId",or.Or_UsId), new SqlParameter("@Or_GmId",or.Or_GmId),
            new SqlParameter("@Or_Date",or.Or_Date)};
            return DBHelper.Instance().ExcuteProcedure("up_AddOrders", para);
        }

       //新增全部
        public static bool AddAllOrders(Orders or)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_UsId",or.Or_UsId), new SqlParameter("@Or_GmId",or.Or_GmId),
                new SqlParameter("@Or_Price",or.Or_Price),new SqlParameter("@Or_Goid",or.Or_Goid),new SqlParameter("@Or_Date",or.Or_Date)};
            return DBHelper.Instance().ExcuteProcedure("up_AddAllOrders", para);
        }

        //删除
        public static bool DeleOrders(int id)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_Id", id) };
            return DBHelper.Instance().ExcuteProcedure("up_DeleOrders", para);
        }
        //删除指定游戏Id内容
        public static bool DeleGmIdOrders(int id)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_GmId", id) };
            return DBHelper.Instance().ExcuteProcedure("up_DeleGmIdOrders", para);
        }
        //删除指定用户订单
        public static bool DeleUsidOrders(int id)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_UsId", id) };
            return DBHelper.Instance().ExcuteProcedure("up_DeleUsidOrders", para);
        }
        //修改全部内容
        public static bool UpdaOrders(Orders or)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@Or_UsId",or.Or_UsId), new SqlParameter("@Or_GmId",or.Or_GmId),
            new SqlParameter("@Or_Date",or.Or_Date),new SqlParameter("@Or_Price",or.Or_Price),new SqlParameter("@Or_Goid",or.Or_Goid),new SqlParameter("@Or_Id",or.Or_Id)};
            return DBHelper.Instance().ExcuteProcedure("up_UpdaOrders", para);
        }
        //查询所有信息
        public static List<Orders> SeleAllOrders()
        {
            List<Orders> item = new List<Orders>();
            string sql = string.Format("select * from Orders ");
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Orders or = new Orders();
                    or.Or_Id = Convert.ToInt32(dt.Rows[i]["Or_Id"].ToString());
                    or.Or_UsId = Convert.ToInt32(dt.Rows[i]["Or_UsId"].ToString());
                    or.Or_GmId = Convert.ToInt32(dt.Rows[i]["Or_GmId"].ToString());
                    or.Or_Price = Convert.ToDouble(dt.Rows[i]["Or_Price"].ToString());
                    or.Or_Goid = Convert.ToInt32(dt.Rows[i]["Or_Goid"].ToString());
                    or.Or_Date = Convert.ToDateTime(dt.Rows[i]["Or_Date"].ToString());
                    item.Add(or);
                }
            }
            return item;
        }
        //查指定订单id信息
        public static List<Orders> SeleIdOrders(int id)
        {
            List<Orders> item = new List<Orders>();
            string sql = string.Format("select * from Orders where Or_Id='{0}'",id);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Orders or = new Orders();
                    or.Or_Id = Convert.ToInt32(dt.Rows[i]["Or_Id"].ToString());
                    or.Or_UsId = Convert.ToInt32(dt.Rows[i]["Or_UsId"].ToString());
                    or.Or_GmId = Convert.ToInt32(dt.Rows[i]["Or_GmId"].ToString());
                    or.Or_Price = Convert.ToDouble(dt.Rows[i]["Or_Price"].ToString());
                    or.Or_Goid = Convert.ToInt32(dt.Rows[i]["Or_Goid"].ToString());
                    or.Or_Date = Convert.ToDateTime(dt.Rows[i]["Or_Date"].ToString());
                    item.Add(or);
                }
            }
            return item;
        }
        //查指定用户id信息
        public static List<Orders> SeleUsIdOrders(int id)
        {
            List<Orders> item = new List<Orders>();
            string sql = string.Format("select * from Orders where Or_UsId='{0}'", id);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Orders or = new Orders();
                    or.Or_Id = Convert.ToInt32(dt.Rows[i]["Or_Id"].ToString());
                    or.Or_UsId = Convert.ToInt32(dt.Rows[i]["Or_UsId"].ToString());
                    or.Or_GmId = Convert.ToInt32(dt.Rows[i]["Or_GmId"].ToString());
                    or.Or_Price = Convert.ToDouble(dt.Rows[i]["Or_Price"].ToString());
                    or.Or_Goid = Convert.ToInt32(dt.Rows[i]["Or_Goid"].ToString());
                    or.Or_Date = Convert.ToDateTime(dt.Rows[i]["Or_Date"].ToString());
                    item.Add(or);
                }
            }
            return item;
        }

       //查询用户下载的游戏
        public static List<Orders> SeleUsGmOrders(int usid,int gmid)
        {
            List<Orders> item = new List<Orders>();
            string sql = string.Format("select * from Orders where Or_UsId='{0}' and Or_GmId='{1}'", usid,gmid);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Orders or = new Orders();
                    or.Or_Id = Convert.ToInt32(dt.Rows[i]["Or_Id"].ToString());
                    or.Or_UsId = Convert.ToInt32(dt.Rows[i]["Or_UsId"].ToString());
                    or.Or_GmId = Convert.ToInt32(dt.Rows[i]["Or_GmId"].ToString());
                    or.Or_Price = Convert.ToDouble(dt.Rows[i]["Or_Price"].ToString());
                    or.Or_Goid = Convert.ToInt32(dt.Rows[i]["Or_Goid"].ToString());
                    or.Or_Date = Convert.ToDateTime(dt.Rows[i]["Or_Date"].ToString());
                    item.Add(or);
                }
            }
            return item;
        }

        //根据用户名查询
        public static List<Orders> SeleLikeUsNameOrders(string name)
        {
            List<Orders> item = new List<Orders>();
            string sql = string.Format("select * from Orders where Or_UsId=(select Us_Id from Users  where Us_Name like '%{0}%')",name);
            DataTable dt = DBHelper.Instance().GetDataTableBySql(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Orders or = new Orders();
                    or.Or_Id = Convert.ToInt32(dt.Rows[i]["Or_Id"].ToString());
                    or.Or_UsId = Convert.ToInt32(dt.Rows[i]["Or_UsId"].ToString());
                    or.Or_GmId = Convert.ToInt32(dt.Rows[i]["Or_GmId"].ToString());
                    or.Or_Price = Convert.ToDouble(dt.Rows[i]["Or_Price"].ToString());
                    or.Or_Goid = Convert.ToInt32(dt.Rows[i]["Or_Goid"].ToString());
                    or.Or_Date = Convert.ToDateTime(dt.Rows[i]["Or_Date"].ToString());
                    item.Add(or);
                }
            }
            return item;
        }

    }
}
